package org.example.forum.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
public class AppResult<T> {
    //状态码
    @JsonInclude(JsonInclude.Include.ALWAYS) //无论什么情况都参与 json 序列化
    private int code;
    //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    //传入数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;


    /**
     * 构造方法
     * @param code
     * @param message
     */
    public AppResult(int code, String message) {
        this(code, message, null);
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }



    /**
     * 成功
     * @return
     */
    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static AppResult success(String message) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }


    public static <T> AppResult success(T data) {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T> AppResult success(String message,T data) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message,data);
    }

    /**
     * 失败
     * @return
     */
    public static AppResult failure() {
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static AppResult failure(String message) {
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }

    public static <T> AppResult failure(ResultCode resultCode) {
        return new AppResult(resultCode.getCode(), resultCode.getMessage());
    }

}
